package hn.cch.mybatis.plus.configuration;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import hn.cch.mybatis.plus.method.CustomSqlInjector;
import org.apache.ibatis.session.ExecutorType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

@MapperScan("hn.cch.mybatis.plus.mapper")
@Configuration
public class MyBatisPlusConfiguration {


    // /**
    //  * 添加拦截器
    //  *
    //  * @return MybatisPlusInterceptor
    //  */
    // @Bean
    // public MybatisPlusInterceptor mybatisPlusInterceptor() {
    //     MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    //     interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    //     interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    //     interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
    //     return interceptor;
    // }
    //
    //
    // /**
    //  * MybatisPlus升级后需要手动注入MybatisSqlSessionFactoryBean
    //  *
    //  * @param dataSource 数据源
    //  * @return MybatisSqlSessionFactoryBean
    //  */
    // @Bean
    // public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
    //     MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    //     sqlSessionFactoryBean.setDataSource(dataSource);
    //
    //
    //     MybatisConfiguration configuration = new MybatisConfiguration();
    //     GlobalConfig globalConfig = GlobalConfigUtils.defaults();
    //     GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
    //     globalConfig.setDbConfig(dbConfig);
    //
    //     // 替换配置文件配置
    //     globalConfig.setSqlInjector(new CustomSqlInjector());
    //     globalConfig.setEnableSqlRunner(true);
    //
    //     configuration.setCacheEnabled(false);
    //     sqlSessionFactoryBean.setConfiguration(configuration);
    //     sqlSessionFactoryBean.setGlobalConfig(globalConfig);
    //
    //     return sqlSessionFactoryBean;
    // }

    /**
     * 自定义SQL注入器
     *
     * @return
     */
    @Bean
    public ISqlInjector iSqlInjector() {
        return new CustomSqlInjector();
    }

}
